<template>
	<view class="mine-container" :style="{ height: `${windowHeight}px` }">
		<view class="menu-list">
			<view class="list-cell">
				<view class="menu-item-boxs">
					<view class="menu-name">勿扰</view>
					<switch :checked="silent" @change="changeSilentMode" />
				</view>
			</view>
			<view class="list-cell">
				<view class="menu-item-boxs">
					<view class="menu-name">调试模式</view>
					<view class="uni-list-cell uni-list-cell-pd">
						<switch :checked="mode.checked" @change="changeMode" />
					</view>
				</view>
			</view>
			<view class="list-cell" v-if="mode.checked">
				<view class="menu-item-box">
					<view class="menu-name"></view>
				</view>
			</view>
			<view class="list-cell">
				<view class="menu-item-box">
					<view class="menu-name">账号</view>
					<view class="menu-net">
						{{ list.loginName == null || list.loginName == "" ? '- -' : list.loginName }}
					</view>
				</view>
			</view>
			<view class="list-cell ">
				<view class="menu-item-box">
					<view class="menu-name">警号</view>
					<view class="menu-net">
						{{ policeNo }}
					</view>
				</view>
			</view>
			<view class="list-cell">
				<view class="menu-item-box">
					<view class="menu-name">版本号</view>
					<view class="menu-net">
						2.0.0
					</view>
					<view class="menu-posit" @click="clearCache()">
						清除缓存
					</view>
				</view>
			</view>
		</view>
		<button style="margin-top: 100rpx;width: 80%;" type="primary" @click="back">退出</button>
		<view class="list-call">
			<view class="menu-title">
				关于
			</view>
			<view class="menu-title">
				本溪市公安局重点人员管控移动警务系统
			</view>
			<view class="menu-title">
				全国注册备案号：{{ registeredNumber }}
			</view>
		</view>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="总线地址" value="" placeholder="请输入地址"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
import { manageUser } from '@/api/mine.js'
export default {
	data() {
		return {
			name: this.$store.state.user.name,
			version: getApp().globalData.config.appInfo.version,
			list: {},
			checked: '',
			mode: {
				checked: ''
			},
			silent: false,
			policeNo: "-",
			registeredNumber: "加载中..."
		}
	},
	computed: {
		avatar() {
			return this.$store.state.user.avatar
		},
		windowHeight() {
			return uni.getSystemInfoSync().windowHeight - 50
		}
	},
	created() {
		var logFlag = localStorage.getItem("logFlag")
		if (logFlag == "1") {
			this.$set(this.mode, 'checked', true)
		} else {
			this.$set(this.mode, 'checked', false)
		}
	},
	onShow() {
		let fante = window["fante"]
		if (fante) {
			uni.showLoading({
				title: '加载中...'
			})
			try {
				this.registeredNumber = fante.getRegisteredNumbers()
				this.policeNo = localStorage.getItem('policeNo') || fante.getPoliceOfficerNumber()
				this.silent = fante.getIsSilent()
			} catch (e) {
				uni.showToast({
					title: 'App信息获取失败',
					duration: 500
				});
			} finally {
				uni.hideLoading()
			}
		}
		this.manageDetail()
	},
	methods: {
		changeSilentMode(e) {
			let value = e.target.value
			let fante = window["fante"]
			if (fante) {
				fante.setIsSilent(value)
			}
		},
		changeMode(e) {
			let value = e.target.value
			if (value) {
				localStorage.setItem("logFlag", 1)
			} else {
				localStorage.setItem("logFlag", 2)
			}
		},
		clearCache() {
			let fante = window["fante"]
			if (fante) {
				try {
					fante.refresh(true)
					uni.showToast({
						title: '缓存清除成功',
						duration: 500
					});
				} catch (e) {
					uni.showToast({
						title: '缓存清除失败',
						duration: 500
					});
				}
			}
		},
		back() {
			let self = this
			uni.showModal({
				title: '退出提示',
				content: '确定要退出系统吗？',
				success: function (res) {
					if (res.confirm) {
						let fante = window["fante"]
						if (fante) {
							uni.showLoading({
								title: '退出中...'
							})
							try {
								fante.setToken("");
								fante.setUserId("");
								this.$store.dispatch('LogOut', {}).then(() => {
									console.log('退出成功')
								}).catch(() => {
									console.log('登录信息注销失败')

								})
							} catch (e) {
								uni.showToast({
									title: 'App登录信息注销失败',
									duration: 500
								});
							} finally {
								uni.hideLoading()
							}
						} else {
							self.$store.dispatch('LogOut', {}).then(() => {
							}).catch(() => {
							})
						}
						uni.navigateTo({
							url: '/pages/login'
						})
					}
				}
			});
		},
		modelLog(e) {
			localStorage.setItem("logFlag", e)
			this.checked = e
		},
		manageDetail() {
			manageUser({
				userId: localStorage.getItem("userId")
			}).then(res => {
				this.list = res.data
			})
		},
		revise() {
			let self = this
			uni.showModal({
				title: '注意',
				content: '此操作会重启此应用，确定要修改吗？',
				success: function (res) {
					if (res.confirm) {
						self.$refs.inputDialog.open()
						self.$nextTick(() => {
							self.$refs.inputClose.value = self.value
						})
					} else if (res.cancel) {

					}
				}
			});

		},
		dialogInputConfirm(val) {
			uni.showLoading({
				title: '修改中...'
			})
			setTimeout(() => {
				let fante = window["fante"]
				if (fante) {
					// 配置总线
					try {
						let result = fante.setStartupUri(val)
						uni.hideLoading()
						if (result) {
							uni.showModal({
								title: '修改失败',
								content: result,
								showCancel: false
							});
						} else {
							uni.showLoading({
								title: '重启应用...'
							})
							this.value = val
							//重启App
							fante.restartApp(true)
						}
					}
					catch (e) {
						uni.showToast({
							title: '总线修改失败',
							duration: 500
						});
					}
				}
				else {
					uni.hideLoading()
				}
				// 关闭窗口后，恢复默认内容
				this.$refs.inputDialog.close()
			}, 100)
		},
	}
}
</script>

<style lang="scss">
page {
	background-color: #fff;
}

.mine-container {
	width: 100%;
	height: 100%;


	.header-section {
		padding: 15px 15px 45px 15px;
		background-color: #3c96f3;
		color: white;

		.login-tip {
			font-size: 18px;
			margin-left: 10px;
		}

		.cu-avatar {
			border: 2px solid #eaeaea;

			.icon {
				font-size: 40px;
			}
		}

		.user-info {
			margin-left: 15px;

			.u_title {
				font-size: 18px;
				line-height: 30px;
			}
		}
	}

	.content-section {
		position: relative;
		top: -50px;

		.mine-actions {
			margin: 15px 15px;
			padding: 20px 0px;
			border-radius: 8px;
			background-color: white;

			.action-item {
				.icon {
					font-size: 28px;
				}

				.text {
					display: block;
					font-size: 13px;
					margin: 8px 0px;
				}
			}
		}
	}
}

.menu-item-boxs {
	display: flex;
	justify-content: space-between;
}

.menu-net {
	width: 330rpx;
	word-wrap: break-word;
}

.menu-name {
	width: 200rpx;
	position: relative;
}

.menu-posit {
	position: absolute;
	right: 52rpx;
	z-index: 10;
	background: #fff;
	color: #3c96f3;
}

.list-call {
	margin-top: 100rpx;
	width: 750rpx;

	.menu-title {
		text-align: center;
		line-height: 50rpx;
	}
}

.reday-span {
	margin-right: 10rpx;
}

.updata-span {
	color: #3c96f3;
}
</style>
